package com.dianrui.firemenmonitor.fragment;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.dianrui.firemenmonitor.Constant;
import com.dianrui.firemenmonitor.util.SPUtils;


import butterknife.ButterKnife;

public abstract class BaseFragment extends Fragment {
    protected Activity mActivity;
    protected LayoutInflater mInflater;
    protected View rootView;
    public SPUtils spUtils;
    public boolean firstLoad = false;

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        mActivity = (Activity) context;
    }


    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        rootView = inflater.inflate(getContentViewId(), container, false);
        spUtils = new SPUtils(getActivity(), Constant.cfgFileName, Context.MODE_PRIVATE);
        ButterKnife.bind(this, rootView);
        initView();
        firstLoad = true;//视图创建完成，将变量置为true
        if (getUserVisibleHint()) {//判断Fragment是否可见
            //初始化数据
            getData();
            firstLoad = false;//将变量置为false
        }
        return rootView;
    }

    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
        if (firstLoad && isVisibleToUser) {//视图变为可见并且是第一次加载
            //初始化数据
            getData();
            firstLoad = false;
        }

    }

    public abstract int getContentViewId();//布局ID

    public abstract void initView();

    public abstract void getData();


    protected void JumpActivitys(Class<?> clazz) {
        Intent intent = new Intent(getActivity(), clazz);
        startActivity(intent);
    }

    protected void JumpKillActivitys(Class<?> clazz) {
        Intent intent = new Intent(getActivity(), clazz);
        startActivity(intent);
        getActivity().finish();
    }


    @Override
    public void onDestroy() {
        super.onDestroy();
        firstLoad = false;//视图销毁将变量置为false
    }
}
